// RedJey Graphics User Interface Library for J2ME Platform.
// Copyright (C) 2008 Yevgen Krapiva
//
// This library is free software; you can redistribute
// it and/or modify it under the terms of the
// GNU Lesser General Public License as published
// by the Free Software Foundation; either version 3.0
// of the License, or any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//

package com.incrediblesoft.redjey;

/**
 * @author Yevgen Krapiva<br>
 *         Homepage: http://code.google.com/p/redjey/<br>
 *         Feedback: ykrapiva@gmail.com<br>
 *         Copyright (C) 2008 Yevgen Krapiva<br>
 */

import com.incrediblesoft.redjey.event.MouseEvent;

import javax.microedition.lcdui.Graphics;
import java.util.Hashtable;

public class MenuItem extends Button
{
    protected Component parentMenu;

    /**
     * Constructs menu item
     * @param title Menu item title
     */
    public MenuItem(String title)
    {
        this(title, null, null);
    }

    /**
     * Constructs menu item with a given title and image
     * @param title Menu item title
     * @param image Menu item icon image
     */
    public MenuItem(String title, CustomImage image)
    {
        this(title, image, null);
    }

    /**
     * Constructs menu item with a given title and image
     * @param title Menu item title
     * @param image Menu item image
     * @param styleClassName Menu item style class name
     */
    public MenuItem(String title, CustomImage image, String styleClassName)
    {
        super(title, image, styleClassName);
    }

    public String getName()
    {
        return "MenuItem";
    }

    public void processMouseEvent(MouseEvent event)
    {
        switch(event.getID())
        {
            case MouseEvent.MOUSE_CLICKED:
                Component root = getRoot();
                if (root != null)
                {
                    if (root instanceof Menu)
                    {
                        //((Menu) root).close();
                        ((MenuBar) ((Menu) root).parent).looseFocus();
                    }
                    else if (root instanceof PopupMenu)
                        ((PopupMenu) root).setVisible(false);

                    super.processMouseEvent(event);
                }
                break;         

            default:
                super.processMouseEvent(event);
        }
    }

    /**
     * Returns the root menu container.
     * Unlike Swing the root can be either Menu or PopupMenu.
     * So use 'instanceof' to check against required.
     * @return Root menu container.
     */
    public Component getRoot()
    {
        Component root = this;

        if (parentMenu != null)
        {
            if (parentMenu instanceof PopupMenu)
                root = ((PopupMenu) parentMenu).getRoot();
        }

        return root;
    }      

    public boolean transferFocusUpCycle()
    {
        Component menu = parentMenu;

        while (menu != null && !(menu instanceof Menu))
            menu = ((PopupMenu) menu).parentMenu;

        if (menu != null)
            ((Menu) menu).close();
        else
            return transferFocusBackward();

        return true;
    }
}
